import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/index' 
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home/index.vue'),
    children:[{
      path: '/index',
      name: 'index',
      component: () => import('../views/home/index/index.vue')
    },
    {
      path: '/carrental',
      name: 'carrental',
      component: () => import('../views/home/carrental/index.vue')
    },
    {
      path: '/order',
      name: 'order',
      meta:{isLogin:true},//表示进入这个页面是需要登录的
      component: () => import('../views/home/order/index.vue')
    }
  ]
  },
  {//改变路由
    path: '/personal',
    name: 'personal',
    meta:{isLogin:true},
    component: () => import('../views/home/personal/index.vue'),
    children:[{
      path: '/personinfo',
      name: 'personinfo',
      meta:{isLogin:true},
      component: () => import('../views/home/personal/infomation/index.vue')
    },
    {
      path: '/personorder',
      name: 'personorder',
      meta:{isLogin:true},
      component: () => import('../views/home/personal/Porder/index.vue')
    }
  ]
    
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/register.vue')
  }
]

const router = new VueRouter({
  routes
})
// 路由守卫
router.beforeEach((to,from,next)=>{
  if(to.matched.some(res=>res.meta.isLogin)){//判断是否需要登录
    if (sessionStorage.getItem('userPhone')) {//判断是否已经登录
      next();
    }else{
      next({
        path:"/login"
      });
    }
  }else{
    next()
  }
});

export default router
